const query = require("../../db/mysql.js");
const express = require("express");
const Router = express.Router();


// 获取总商品类型 type 即可获取相对应数据
Router.get("/allType", async (req, res) => {
    let data = {};
    let {type} = req.query;
    type = type || '' ;
    try {
        let sql = `select * from totalType where type like '%${type}%'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})
//分页获取总商品类型
Router.get('/padingallType', async (req, res) => {
    let { page, size } = req.query;
    page = page || 1;
    size = size || 5;
    let data = {};
    let index = (page - 1) * size;
    try {
        let sql = `select * from totalType where typeId limit ${index},${size}`;
        let p = await query(sql);
        let sqltotal = `select * from totalType`;
        let arr = await query(sqltotal);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                total:arr.length,
                page,//当前页
                size,//一页多少条
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})
//增加总商品类型
Router.post("/addAllType", async (req, res) => {
    let data = {};
    let { type } = req.body;
    try {
        let sql = `insert into totaltype(type) value('${type}')`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '增加成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '增加失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})

//删除总商品类型
Router.delete("/delAllType", async (req, res) => {
    let data = {};
    let { typeId } = req.body;
    try {
        let sql = `delete from totaltype where typeId='${typeId}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '删除成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '删除失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
    res.send(data)
})

//更改总商品类型
Router.put("/updataAllType", async (req, res) => {
    let data = {};
    let { typeId, newType } = req.body;
    try {
        let sql = `update totaltype set type='${newType}' where typeId='${typeId}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功',
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data)
})

//获取商品类型  传入 tid获取相应总类型的商品类型，不传获取全部商品类型
Router.get("/getType", async (req, res) => {
    let data = {};
    let { tid } = req.query;
    try {
        let sql = tid ? `select * from goodstype where tid='${tid}'` : `select * from goodstype`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})
// 查询商品类型
Router.get("/getgoodsType", async (req, res) => {
    let data = {};
    let { tid,kname } = req.query;
    tid = tid || '';
    kname = kname || "";
    try {
        let sql = `select * from goodstype where tid like '%${tid}%' and kname like '%${kname}%'`;
        let p = await query(sql);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})
//分页获取商品类型
Router.get('/padinggoodsType', async (req, res) => {
    let { page, size } = req.query;
    page = page || 1;
    size = size || 5;
    let data = {};
    let index = (page - 1) * size;
    try {
        let sql = `select * from goodstype where tid limit ${index},${size}`;
        let p = await query(sql);
        let sqltotal = `select * from goodstype`;
        let arr = await query(sqltotal);
        if (p.length) {
            data = {
                code: 2000,
                flag: true,
                message: '获取成功',
                total:arr.length,
                page,//当前页
                size,//一页多少条
                data: p
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '获取失败',
            }
        }
        res.send(data)
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错',
        }
    }
})
//增加商品类型
Router.post("/addType", async (req, res) => {
    let data = {};
    let { tid,kname, kimg } = req.body;
    try {
        let sql = `insert into goodstype(tid,kname,kimg) value('${tid}','${kname}','${kimg}')`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '增加成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '增加失败'
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})

//删除商品类型
Router.delete("/delType", async (req, res) => {
    let data = {};
    let { kid } = req.body
    try {
        let sql = `delete from goodstype where kid='${kid}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '删除成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '删除失败'
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})

//修改商品类型
Router.put("/editType/:kid", async (req, res) => {
    let data = {};
    let { kid } = req.params;
    let str = ``;
    for (let key in req.body) {
        str += key + '=' + `'${req.body[key]}'` + ','
    }
    str = str.slice(0, -1);
    try {
        let sql = `update goodstype set ${str} where kid='${kid}'`;
        let p = await query(sql);
        if (p.affectedRows) {
            data = {
                code: 2000,
                flag: true,
                message: '修改成功'
            }
        } else {
            data = {
                code: 3000,
                flag: false,
                message: '修改失败'
            }
        }
    } catch (err) {
        data = {
            code: 5000,
            flag: false,
            message: '服务器出错'
        }
    }
    res.send(data);
})

module.exports = Router;                      